package com.DongBing.service.impl;

import com.DongBing.common.utils.Res;
import com.DongBing.dao.BrandDao;
import com.DongBing.dao.CategoryDao;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.DongBing.common.utils.PageUtils;
import com.DongBing.common.utils.Query;

import com.DongBing.dao.CategoryBrandRelationDao;
import com.DongBing.entity.CategoryBrandRelationEntity;
import com.DongBing.service.CategoryBrandRelationService;
import org.springframework.util.ObjectUtils;


@RequiredArgsConstructor
@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    private final CategoryDao categoryDao;

    private final BrandDao brandDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        val page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Res<?> findCategoryBrandList(String brandId) {
        val wrapper = new LambdaQueryWrapper<CategoryBrandRelationEntity>();
        wrapper.eq(!ObjectUtils.isEmpty(brandId),CategoryBrandRelationEntity::getBrandId,brandId);
        val list = baseMapper.selectList(wrapper);
        return Res.success("查询成功",list);
    }

    @Override
    public Res<?> insert(CategoryBrandRelationEntity bean) {
        val brandId = bean.getBrandId();
        val cateLogId = bean.getCatelogId();
        val categoryEntity =categoryDao.selectById(cateLogId);
        val brandEntity = brandDao.selectById(brandId);
        bean.setBrandName(brandEntity.getName());
        bean.setCatelogName(categoryEntity.getName());
        baseMapper.insert(bean);
        return Res.success("添加成功");
    }

}
